/*
 * mainPanel.java
 *
 * Created on 6 novembre 2007, 22:46
 */

package jtalk.view;

import java.beans.PropertyChangeEvent;

import jtalk.controller.DefaultController;

import jtalk.controller.Properties;
import jtalk.view.ressources.StatusIcons;


/**
 *
 * @author  remy
 */
public class MainPanel extends AbstractViewPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 986537474922412872L;
	/** Creates new form mainPanel */
    public MainPanel(DefaultController controller) {
    	this.controller = controller;
        initComponents();
        init();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        statusBarPane = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        mainScrollPane = new javax.swing.JScrollPane();
        scrolledMainPanel = new javax.swing.JPanel();

        statusBarPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        statusLabel.setIcon(jtalk.view.ressources.StatusIcons.ICON_RED);
        statusLabel.setText("disconnected");

        javax.swing.GroupLayout statusBarPaneLayout = new javax.swing.GroupLayout(statusBarPane);
        statusBarPane.setLayout(statusBarPaneLayout);
        statusBarPaneLayout.setHorizontalGroup(
            statusBarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
        );
        statusBarPaneLayout.setVerticalGroup(
            statusBarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout scrolledMainPanelLayout = new javax.swing.GroupLayout(scrolledMainPanel);
        scrolledMainPanel.setLayout(scrolledMainPanelLayout);
        scrolledMainPanelLayout.setHorizontalGroup(
            scrolledMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 281, Short.MAX_VALUE)
        );
        scrolledMainPanelLayout.setVerticalGroup(
            scrolledMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 407, Short.MAX_VALUE)
        );
        mainScrollPane.setViewportView(scrolledMainPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusBarPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(mainScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(mainScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusBarPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void init() {
        connectionPanel = new ConnectionPanel(controller);
        mainScrollPane.setViewportView(connectionPanel);                      
        controller.addView(connectionPanel);
        
        contactListPanel = new ContactListPanel(controller);
        controller.addView(contactListPanel);
    }

    public void modelPropertyChange(PropertyChangeEvent evt) {
        
    	//Connection Events
    	
        if(evt.getPropertyName().equals(Properties.CONNECTED.toString())) {
            if((Boolean)evt.getNewValue()) {
            	//Update status bar
                statusLabel.setText("connected");
                statusLabel.setIcon(StatusIcons.ICON_YELLOW);
            }else {
            	//Update status bar
                statusLabel.setText("disconnected");
                statusLabel.setIcon(StatusIcons.ICON_RED);
                
                //Display connection parameters
                mainScrollPane.setViewportView(connectionPanel);
            }
        }else if(evt.getPropertyName().equals(Properties.AUTHENTICATED.toString())) {
            if((Boolean)evt.getNewValue()) {
            	//Update status bar
                statusLabel.setText("authenticated");
                statusLabel.setIcon(StatusIcons.ICON_GREEN);
                
                //Display contact list
                mainScrollPane.setViewportView(contactListPanel);
                
            }else {
            	//Update status bar
                statusLabel.setText("not authenticated");
                statusLabel.setIcon(StatusIcons.ICON_YELLOW);
                
                //Display connection parameters
                mainScrollPane.setViewportView(connectionPanel);
            }
        }
    }
    
    public DefaultController getController() {
    	return controller;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane mainScrollPane;
    private javax.swing.JPanel scrolledMainPanel;
    private javax.swing.JPanel statusBarPane;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
    
    //Other variables
    private DefaultController controller;
    private ConnectionPanel connectionPanel;
    private ContactListPanel contactListPanel;
    //End
    
}
